/******************************************************************************\
*  Animeshell 1.0 Multimedia manager                                           *
*  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
*                                                                              *
*  This program is free software; you can redistribute it and/or modify        *
*  it under the terms of the GNU General Public License version 2 only as      *
*  published by the Free Software Foundation.                                  *
*                                                                              *
*  This program is distributed in the hope that it will be useful,             *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
*  GNU General Public License for more details.                                *
*                                                                              *
*  You should have received a copy of the GNU General Public License           *
*  along with this program; if not, write to the                               *
*  Free Software Foundation, Inc.,                                             *
*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
\******************************************************************************/

#include <QFile>
#include <QProcess>
#include <QStringList>
#include <QDesktopServices>
#include <QUrl>

#include "imgcontrol.h"


bool imgcontrol::copyimg(QString pSrc, QString pDest, QString pNameImg)
{
	if ( pSrc == "" || pDest == "" || pNameImg == "" )
		return false;
	
	QString newName(pDest + '/' + pNameImg);
	
	return QFile::copy(pSrc, newName);
}



bool imgcontrol::eraseimg(QString pSrc)
{
	
	if ( !QFile::exists(pSrc) )
	{
		return false;
	}
	
	return QFile::remove(pSrc);
}



void imgcontrol::startprocess(QString pEditor, QString pNameImg)
{
	
	if ( !(searchpath(pEditor)).isNull() )
	{
		QProcess *MyProcess = new QProcess();
		MyProcess->start(pEditor, QStringList() << pNameImg);
	}
}


void imgcontrol::startprocess(QString pNameImg)
{
	if ( !pNameImg.isNull() )
	{
		QDesktopServices::openUrl(QUrl(pNameImg));
	}
}



QString imgcontrol::searchpath(QString pProcessName)
{
	QStringList PathList = QProcess::systemEnvironment();
	QStringList SeparatePath;
	QString ProcesPath;
	int i = 0, j = 0;
	QString ret;
	
	if ( pProcessName == "" || pProcessName.isNull() )
		return QString::null;
	
	while ( i < PathList.size() )
	{
		// Find PATH variable
		if ( (PathList.at(i)).indexOf("PATH", 0, Qt::CaseSensitive) == 0 )
		{
			ProcesPath = PathList.at(i);
			
			// Remove "PATH="
			ProcesPath.remove(0, 5);
			
			// Split each string
			SeparatePath = ProcesPath.split(":", QString::SkipEmptyParts);
			
			// Search process name
			while ( j < SeparatePath.size() )
			{
				if ( QFile::exists(SeparatePath.at(j) + "/" + pProcessName) )
				{
					ret = SeparatePath.at(j) + "/" + pProcessName;
					i = PathList.size();
					j = SeparatePath.size();
				}
				j ++;
			}
		}
		i ++;
	}
	
	return ret;
}
//
